#include <cstddef>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <queue>
using namespace std;
vector<vector<int>> g(1500, vector<int>(1500, 0));
vector<int> in(1500);
vector<int> depth(1500);
int main() {
    int n, m;
    cin >> n >> m;
    int L = 0x3f3f3f;
    int R = -1;
    for(int i = 0; i < m; i++) {
        vector<int> temp;
        vector<int> temp2;
        unordered_map<int, bool> h;
        int s;
        cin >> s;
        for(int i = 0; i < s; i++) {
            int t;
            cin >> t;
            h[t] = true;
            temp.push_back(t);
        }
        L = min(L, temp[0]);
        R = max(R, temp[temp.size() - 1]);
        for(int i = temp[0]; i <= temp[temp.size() - 1]; i++)
        {
            if(!h[i]) {
                temp2.push_back(i);
            }
        }
        for(auto& x : temp) {
            for(auto& y : temp2) {
                if(g[x][y] == 0) {
                    g[x][y] = 1;
                    in[y]++;
                } 
            }
        }
    }
    queue<int> q;
    for(int i = L; i <= R; i++) {
        if(in[i] == 0) {
            depth[i] = 1;
            q.push(i);
        }
    }
    while(!q.empty()) {
        int k = q.front();
        q.pop();
        for(int i = L; i <= R; i++) {
            if(g[k][i]) {
                depth[i] = max(depth[i], depth[k] + 1);
                in[i]--;
                if(in[i] == 0) {
                    q.push(i);
                }
            }
        }
    }
    int ret = -1;
    for(int i = L; i <= R; i++) {
        ret = max(ret, depth[i]);
    }
    cout << ret;
    return 0;
}